<?php

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();


// $mdb=MaintenanceShops::where("status",1)->get();
// foreach ($mdb as $_mdb) {//计算每个点的营业额
// 	$data = ReportTask::operating_rule($_mdb->id,date("Y-m-d",time()));
// }
// ReportTask::operating_rule(1,'2016-08-30');
DB::connection()->disableQueryLog(); 

// $mdb=MaintenanceShops::all();
 $mdb=MaintenanceShops::wherein("id",array(6))->get();
// $mdb=MaintenanceShops::where("status",1)->get();

$_date=array("2018-07-31","2018-07-31");
$cur_date=floor((strtotime($_date[1])-strtotime($_date[0]))/86400);
for($i=0;$i<=$cur_date;$i++){  
    $date=date('Y-m-d',strtotime($_date[0])+$i*86400);   
    foreach ($mdb as $_mdb) {//
		// $date=date('Y-m-d',time());
		// ReportTask::operating_rule($_mdb->id,$date);

		$begin_date=date('Y-m-d 00:00:00',strtotime($date));
		$end_date=date('Y-m-d 23:59:59',strtotime($date));
		$begin_date_new=date("Y-m-d H:i:s",strtotime($date." 00:00:00")-Config::get("config.report_time_diff"));
		$end_date_new=date("Y-m-d H:i:s",strtotime($date." 23:59:59")-Config::get("config.report_time_diff"));
		




		$data = Report::get_export_detail($begin_date,$end_date,$_mdb->id);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_export_detail")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_export_detail";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();


		//计算 get_out_detail;
		$data = Report::get_out_detail($begin_date,$end_date,$_mdb->id);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_out_detail")->first();
		if(is_null($rpr)){
		    $rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_out_detail";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();


		//计算 get_profit_report 毛利率分析
		$data = Report::get_profit_report($begin_date,$end_date,$_mdb->id);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_profit_report")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_profit_report";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();


		//计算 get_work_hour_report 订单工时分类明细
		$data = Report::get_work_hour_report($begin_date,$end_date,$_mdb->id);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_work_hour_report")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_work_hour_report";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();


		//计算 get_work_hour_product 服务营业分析
		$data = Report::get_work_hour_product($begin_date,$end_date,$_mdb->id);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_work_hour_product")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_work_hour_product";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 get_order_income 订单收入明细
		$data = Report::get_order_income($begin_date,$end_date,$_mdb->id);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_order_income")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_order_income";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存入库数据
		$data =Report::get_storage_data_for_jxc($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_storage_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_storage_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存服务订单维修出库
		$data = Report::get_service_order_out_data_for_jxc($_mdb->id,0,$begin_date,$end_date);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_service_order_out_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_service_order_out_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存零售订单销售出库
		$data = Report::get_sale_order_out_data_for_jxc($_mdb->id,0,$begin_date,$end_date);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_sale_order_out_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_sale_order_out_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存门店间内部调出出库
		$data = Report::get_transfer_out_data_for_jxc($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_transfer_out_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_transfer_out_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存门店间内部调出出库 领用
		$data = Report::get_transfer_out_data_for_jxc_ly($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_transfer_out_data_for_jxc_ly")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_transfer_out_data_for_jxc_ly";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存门店间内部调出出库 调出
		$data = Report::get_transfer_out_data_for_jxc_dc($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_transfer_out_data_for_jxc_dc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_transfer_out_data_for_jxc_dc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存门店间内部调出出库 销售
		$data = Report::get_transfer_out_data_for_jxc_xs($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_transfer_out_data_for_jxc_xs")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_transfer_out_data_for_jxc_xs";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存供货商退货出库
		$data = Report::get_supplier_out_data_for_jxc($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_supplier_out_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_supplier_out_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存盘点盘盈调整入库
		$data = Report::get_inventory_storage_data_for_jxc($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_inventory_storage_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_inventory_storage_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存盘点盘亏调整出库
		$data = Report::get_inventory_out_data_for_jxc($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_inventory_out_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_inventory_out_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		//计算 进销存结算后退货的
		$data = Report::get_return_storage_data_for_jxc($_mdb->id,0,$begin_date_new,$end_date_new);
		$data = base64_encode(gzcompress(serialize($data),9));
		$rpr=ReportDateReocrd::where("date",$date)->where("shop_id",$_mdb->id)->where("report_type","get_return_storage_data_for_jxc")->first();
		if(is_null($rpr)){
			$rpr=new ReportDateReocrd;
		}
		$rpr->shop_id=$_mdb->id;
		$rpr->report_type="get_return_storage_data_for_jxc";
		$rpr->date=$date;
		$rpr->data=$data;
		$rpr->save();

		Log::info("门店".$_mdb->id." 日期 ".$date.'完毕');
		echo "门店".$_mdb->id." 日期 ".$date.'完毕';
	}

	Log::info(" 日期 ".$date.'完毕');
	echo " 日期 ".$date.'完毕';
} 

 ?>